//
//  LPCostomChooseYearAndMonthAndDayAndHourAndMintueAndMiao.m
//  GongXinDangJian
//
//  Created by 刘应平 on 2017/12/8.
//  Copyright © 2017年 apple. All rights reserved.
//

#import "LPCostomChooseYearAndMonthAndDayAndHourAndMintueAndMiao.h"
#import "NSCalendar+LPH.h"
#define ConfirmButtonTitleColor         [UIColor colorWithRed:0.26f green:0.56f blue:1.00f alpha:1.00f]

@interface LPCostomChooseYearAndMonthAndDayAndHourAndMintueAndMiao ()<UIPickerViewDelegate,UIPickerViewDataSource>
/** 1.年 */
@property (nonatomic, assign)NSInteger year;
/** 2.月 */
@property (nonatomic, assign)NSInteger month;
/** 3.日 */
@property (nonatomic, assign)NSInteger day;
/**  时  */
@property (assign, nonatomic) NSInteger  hour;
/**  fen  */
@property (assign, nonatomic) NSInteger  mintue;
/**  秒  */
@property (assign, nonatomic) NSInteger  miao;


/** 4.上午、下午、晚上   */
@property (strong, nonatomic) NSString   *string;

@property(nonatomic,strong)UIPickerView  * pickerView;
@property(nonatomic,strong)UIView * baseView;

@property(nonatomic,strong)UILabel * titleLabel;
@property(nonatomic,strong)UIButton * cancelBtn;
@property(nonatomic,strong)UIButton * sureBtn;

@property(nonatomic,strong)NSString * dateString;
/**  日期数组 这是除了年月日之外重新加的数据，列如：上午，下午，晚上 */
@property (strong, nonatomic) NSArray  *dateArray;
@end
@implementation LPCostomChooseYearAndMonthAndDayAndHourAndMintueAndMiao

-(instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor zj_colorWithR:20 G:20 B:20 Al:0.3];
        
    }
    return self;
}
-(void)setUI:(NSString *)time{
    _yearLeast = 1900;
    _yearSum   = 200;
    _heightPickerComponent = 28;
    
    if (time.length == 0) {
        _year  = [NSCalendar currentYear];
        _month = [NSCalendar currentMonth];
        _day   = [NSCalendar currentDay];
        _hour = [NSCalendar currentHour];
        _mintue = [NSCalendar currentMintue];
        _miao = [NSCalendar currentSecond];
    }else{
        _year = [[time substringToIndex:5] integerValue];
        _month = [[time substringWithRange:NSMakeRange(5, 2)] integerValue];
        _day = [[time substringWithRange:NSMakeRange(8, 2)] integerValue];
        _hour = [[time substringWithRange:NSMakeRange(11, 2)] integerValue];
        
        NSString *min = [time substringFromIndex:14];
        _mintue = [min integerValue];
        _miao = [[time substringFromIndex:17] integerValue];
    }
    
    
    [self addTitleAndBtn];
    
    [self addSubview:self.baseView];
    [self.baseView addSubview:self.pickerView];
    
    [self.pickerView setDelegate:self];
    [self.pickerView setDataSource:self];
    
    [self.pickerView selectRow:(_year - _yearLeast) inComponent:0 animated:NO];
    [self.pickerView selectRow:(_month - 1) inComponent:1 animated:NO];
    [self.pickerView selectRow:(_day - 1) inComponent:2 animated:NO];
    [self.pickerView selectRow:(_hour) inComponent:3 animated:NO];
    [self.pickerView selectRow:(_mintue) inComponent:4 animated:NO];
    [self.pickerView selectRow:(_miao) inComponent:5 animated:NO];
    
}
#pragma mark  pickerView的代理方法

-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
    if (component == 0) {
        return self.yearSum;
    }else if(component == 1) {
        return 12;
    }else if(component == 2) {
        NSInteger yearSelected = [pickerView selectedRowInComponent:0] + self.yearLeast;
        NSInteger monthSelected = [pickerView selectedRowInComponent:1] + 1;
        return  [NSCalendar getDaysWithYear:yearSelected month:monthSelected];
    }else if (component == 3){
        return 24;
    }else if (component == 4){
        return 60;
    }else{
        return 60;
    }
}

/**  组的个数  */
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
    return 6;
}
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
    [pickerView reloadAllComponents];
    [self reloadData];
}
/**  修改pickview显示的字体大小  */
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view{
    UILabel* pickerLabel = (UILabel*)view;
    if (!pickerLabel){
        pickerLabel = [[UILabel alloc] init];
        // Setup label properties - frame, font, colors etc
        //adjustsFontSizeToFitWidth property to YES
        pickerLabel.adjustsFontSizeToFitWidth = YES;
        pickerLabel.textAlignment = NSTextAlignmentCenter;
        [pickerLabel setBackgroundColor:[UIColor clearColor]];
        [pickerLabel setFont:[UIFont boldSystemFontOfSize:18]];
    }
    // Fill the label text here
    pickerLabel.text=[self pickerView:pickerView titleForRow:row forComponent:component];
    return pickerLabel;
}

/**  单元格的高度  */
- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component{
    
    return 40;
}
- (void)reloadData
{
    self.year  = [self.pickerView selectedRowInComponent:0] + self.yearLeast;
    self.month = [self.pickerView selectedRowInComponent:1] + 1;
    self.day   = [self.pickerView selectedRowInComponent:2] + 1;
    self.hour = [self.pickerView selectedRowInComponent:3] ;
    self.mintue   = [self.pickerView selectedRowInComponent:4] ;
    self.miao   = [self.pickerView selectedRowInComponent:5] ;
    
}

/**  选择完成之后的返回的数据  */
-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
    NSString *text;
    if (component == 0) {
        text =  [NSString stringWithFormat:@"%ld", row + 1900];
    }else if (component == 1){
        text =  [NSString stringWithFormat:@"%ld", row + 1];
    }else if(component == 2){
        text = [NSString stringWithFormat:@"%ld", row + 1];
    }else if (component == 3){
        text = [NSString stringWithFormat:@"%ld", row];
    }else if(component == 4){
        text = [NSString stringWithFormat:@"%ld", row];
    }else{
        text = [NSString stringWithFormat:@"%ld", row];
    }
    
    _dateString = [NSString stringWithFormat:@"%ld-%02ld-%02ld %02ld:%02ld:%02ld",self.year,self.month,self.day,self.hour,self.mintue,self.miao];

    
    return text;
}

/**  添加头部  */
-(void)addTitleAndBtn{
    
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.bounds.size.width, 40)];
    view.backgroundColor = [UIColor viewBGGrayColor];
    [self.baseView addSubview:view];
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 10, self.bounds.size.width, 20)];
    label.text = @"选择时间";
    label.textAlignment = NSTextAlignmentCenter;
    [self.baseView addSubview:label];
    NSArray * btnTitle = @[@"取消",@"确定"];
    for (int i = 0; i < 2; i++) {
        UIButton * btn = [UIButton buttonWithType:UIButtonTypeCustom];
        [btn setTitle:btnTitle[i] forState:UIControlStateNormal];
        btn.frame = CGRectMake((i == 0)?0:(self.bounds.size.width - 60), 0, 60, 40);
        if (i == 0) {
            _cancelBtn = btn;
        }else
            _sureBtn = btn;
        [btn setTitleColor:ConfirmButtonTitleColor forState:UIControlStateNormal];
        [btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
        btn.titleLabel.font = [UIFont systemFontOfSize:15];
        [self.baseView addSubview:btn];
    }
    
    
}
/** 点击事件  */
-(void)btnClick:(UIButton *)btn{
    if (btn == _cancelBtn) {
        [self hiden];
    }else{
        
        if (self.delegate) {
            [self.delegate LPCostomChooseYearAndMonthAndDayAndHourAndMintueAndMiaoWithTitle:_dateString];
        }
        [self hiden];
    }
    btn.selected = !btn.selected;
}

-(UIPickerView *)pickerView{
    if (!_pickerView) {
        _pickerView = [[UIPickerView alloc]initWithFrame:CGRectMake(0, 40, self.bounds.size.width, self.baseView.frame.size.height - 40)];
        _pickerView.showsSelectionIndicator = YES;
    }
    return _pickerView;
}
- (UIView *)baseView{
    if (!_baseView) {
        _baseView = [[UIView alloc] initWithFrame:CGRectMake(0, self.bounds.size.height - 240, self.bounds.size.width, 240)];
        _baseView.backgroundColor = [UIColor whiteColor];
    }
    return _baseView;
}
-(void)hiden{
    [self removeFromSuperview];
}
-(void)show:(NSString *)time{
    [self setUI:time];
    [[UIApplication sharedApplication].keyWindow addSubview:self];
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    
    //    [self hiden];
}

@end
